﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;

using System.Windows.Forms;

namespace CXJ.BLL
{
    public class tbAdmin
    {
        public void SelectData(DataGridView dgView)
        {
            DataSet ds = CXJ.Libiary.DataFactory.Create(typeof(CXJ.Model.tbAdminEntity)).Select();
            dgView.DataSource = ds.Tables[0];
        }

        public void InsertData(CXJ.Model.tbAdminEntity _Entity)
        {
            Interface.IDataOperater pDataOperater = CXJ.Libiary.DataFactory.Create(_Entity.GetType());
            bool ret = pDataOperater.Operate(_Entity, CXJ.Interface.OperateMode.Insert);
            if (ret)
            {
                Libiary.MsgDefault.Success_Insert();
            }
            else
            {
                Libiary.MsgDefault.Exception(pDataOperater.ErrMsg.ToString());
            }
        }
        public void UpdateData(CXJ.Model.tbAdminEntity _Entity)
        {
            if (_Entity == null) return;
            Interface.IDataOperater pDataOperater = CXJ.Libiary.DataFactory.Create(_Entity.GetType());
            bool ret = pDataOperater.Operate(_Entity, CXJ.Interface.OperateMode.Update);
            if (ret)
            {
                Libiary.Msg.Hint("数据更新成功!");
            }
            else
            {
                Libiary.Msg.Warning("数据更新失败!原因:" + pDataOperater.ErrMsg.ToString());
            }
        }

        public void DeleteData(DataGridView dgView)
        {
            CXJ.Model.tbAdminEntity _Entity = new CXJ.Model.tbAdminEntity();
            Interface.IDataOperater pDataOperater = CXJ.Libiary.DataFactory.Create(_Entity.GetType());

            if (dgView.SelectedRows.Count > 0)
            {
                DataGridViewRow dr = dgView.SelectedRows[0];

                _Entity = new CXJ.Model.tbAdminEntity
                {
                    iadmID = dr.Cells["iadmID"].Value.ToString(),
                    sUserName = dr.Cells["sUserName"].Value.ToString(),
                    sPassword = dr.Cells["sPassword"].Value.ToString(),
                    iPermission = dr.Cells["iPermission"].Value.ToString(),
                    dDateTime = DateTime.Now.ToString(),
                };
            }

            bool ret = pDataOperater.Operate(_Entity, CXJ.Interface.OperateMode.Delete);
            string msg = ret ? "删除成功" : "删除失败";
            MessageBox.Show(msg);
        }

        public void SelectDataWithCondition(DataGridView dgView)
        {
            CXJ.Model.tbAdminEntity _Entity = new CXJ.Model.tbAdminEntity() { sUserName = "adminis" };
            DataSet ds = CXJ.Libiary.DataFactory.Create(typeof(CXJ.Model.tbAdminEntity)).Select(_Entity);
            dgView.DataSource = ds.Tables[0];
        }
    }
}
